package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.domain.MesEquipment;
import com.dnjn.mes.biz.domain.MesRegisterAddress;
import com.dnjn.mes.biz.domain.RunStatus;
import com.dnjn.mes.biz.enums.BoolType;
import com.dnjn.mes.biz.assist.ext.Wrapperz;
import com.dnjn.mes.biz.service.IMesEquipmentService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Tag(name = "mes equipment 设备", description = "设备")
@RestController
@RequestMapping("/biz/mesEquipment")
public class MesEquipmentController extends BaseController {

    @Autowired
    protected IMesEquipmentService baseService;

    @GetMapping("/current")
    public AjaxResult current() {
        return success(baseService.getOne(Wrapperz.lambdaQuery(MesEquipment.class).eq(MesEquipment::getIsDefault, BoolType.YES)));
    }

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesEquipment domain) {
        long cnt = baseService.count(Wrapperz.lambdaQuery(MesEquipment.class).eq(MesEquipment::getEquipmentName, domain.getEquipmentName()));
        Assert.isTrue(cnt == 0, "mes.equipment.exist");
        boolean flag = baseService.save(domain);
        return toAjax(flag);
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesEquipment domain) {
        Assert.notNull(domain.getEquipmentId(), "mes.invalid.equipmentId");
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestBody MesEquipment domain, @RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "10") Integer size,
                           @RequestParam(defaultValue = "equipment_id") String orderByDesc) {
        Page page = Page.of(current, size);
        Page<MesEquipment> list = baseService.page(page, Wrappers.<MesEquipment>query(domain).orderByDesc(orderByDesc));
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, MesEquipment domain) {
        List<MesEquipment> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesEquipment> util = new ExcelUtil<MesEquipment>(MesEquipment.class);
        util.exportExcel(response, list, "参数数据");
    }

    @Operation(summary ="通讯设置")
    @PutMapping("/cationSetting")
    public AjaxResult cationSetting(@RequestParam(required = true) Long equipmentId,@RequestParam String modbusIp,Integer modbusPort) {
        Assert.notNull(equipmentId, "mes.invalid.equipmentId");
        Assert.notNull(modbusIp, "IP不能为空！");
        Assert.notNull(modbusPort, "端口号不能为空！");
        MesEquipment mesEquipment = new MesEquipment();
        mesEquipment.setEquipmentId(equipmentId);
        mesEquipment.setModbusIp(modbusIp);
        mesEquipment.setModbusPort(modbusPort);
        return toAjax(baseService.updateById(mesEquipment));
    }


    @Operation(summary ="基础信息节拍设置")
    @PutMapping("/beatSetting")
    public AjaxResult beatSetting(@RequestParam(required = true) Long equipmentId,@RequestParam Integer defaultBeat) {
        Assert.notNull(equipmentId, "mes.invalid.equipmentId");
        Assert.notNull(defaultBeat, "defaultBeat默认节拍不能为空");
        MesEquipment mesEquipment = new MesEquipment();
        mesEquipment.setEquipmentId(equipmentId);
        mesEquipment.setDefaultBeat(defaultBeat);
        return toAjax(baseService.updateById(mesEquipment));
    }



}